package main.java.indi.zyj.c150.back;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class _05_Permute {

    List<List<Integer>> res = new ArrayList<>();

    LinkedList<Integer> track = new LinkedList<>();

    boolean[] used;

    public List<List<Integer>> permute(int[] nums) {
        used = new boolean[nums.length];
        backtrack(nums);
        return res;
    }

    void backtrack(int[] nums) {
        if (nums.length == track.size()){
            res.add(new LinkedList<>(track));
            return;
        }


        for (int i = 0; i < nums.length; i++) {
            if (used[i]){
                continue;
            }
            track.add(nums[i]);
            used[i] = true;
            backtrack(nums);
            used[i] = false;
            track.removeLast();
        }

    }


}
